package com.jeeplus.modules.guide.utils;

import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * ClassName: DistanceUtils
 * Function:  经纬度距离计算
 * Date:      2021/6/3 15:28
 *
 * @author hanKongbin
 * version    V1.0
 */
public class DistanceUtils {

    static double DEF_PI = 3.14159265359D;
    static double DEF_2PI = 6.28318530712D;
    static double DEF_PI180 = 0.01745329252D;
    static double DEF_R = 6370693.5D;


    /**
     * 获取直线距离
     *
     * @param p1
     * @param p2
     * @return
     */
    public static double getDistance(Double[] p1, Double[] p2) {
        double ew1 = p1[0] * DEF_PI180;
        double ns1 = p1[1] * DEF_PI180;
        double ew2 = p2[0] * DEF_PI180;
        double ns2 = p2[1] * DEF_PI180;

        double dew = ew1 - ew2;

        if (dew > DEF_PI) {
            dew = DEF_2PI - dew;
        } else if (dew < -DEF_PI) {
            dew = DEF_2PI + dew;
        }
        double dx = DEF_R * Math.cos(ns1) * dew;
        double dy = DEF_R * (ns1 - ns2);
        double distance = Math.sqrt(dx * dx + dy * dy);
        return distance * 0.001D;
    }

    public static void main(String[] args) {
        // 四舍五入
        System.out.println(Math.round(getDistance(new Double[]{Double.valueOf("104.153993"), Double.valueOf("30.607771")},new Double[]{Double.valueOf("104.239601"), Double.valueOf("30.570651")}) * 100.0D) / 100.0D);
    }


}
